package com.estar.assistsurvy.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.R.color;
import com.estar.assistsurvy.entity.RequestForgetpwd;
import com.estar.assistsurvy.entity.RequestMsg;
import com.estar.assistsurvy.entity.ResponseMsg;
import com.estar.assistsurvy.thread.ForgetpwdThread;
import com.estar.assistsurvy.thread.GetCodeThread;
import com.estar.assistsurvy.utils.CheckInfoUtils;
import com.estar.assistsurvy.utils.MD5Utils;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ForgetPwdActivity extends BaseActivity implements OnClickListener,TextWatcher{

	private TextView returnBack;
	private EditText phoneNum,code,pwd;
	private Button getCode,submit;
	private SharedPreferences preference;
	private String UUID,
	deviceUUID,
	phoneNumber,
	password,
	deviceModel="1",//设备类型，1代表安卓
	verifyCode,
	fromType="1";
	private Timer timer;
	private int timeOut=60;
	private ProgressDialog dialog;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
				
			case 0:
				ResponseMsg message1=new ResponseMsg();
				message1=(ResponseMsg) msg.obj;
				if(message1.getCode().equals("1")&&timer==null && timeOut>0){
					timer=new Timer();
					timer.schedule(new MyTimerTask(), 0, 1000);
				}
				showShortToast(message1.getMessages());
				break;
				
			case 1:
				dialog.dismiss();
				ResponseMsg message2=new ResponseMsg();
				message2=(ResponseMsg) msg.obj;
				showShortToast(message2.getMessages());
				if(message2.getCode().equals("1")){
					finish();
				}
				break;
				
			case 2:
				getCode.setText(timeOut+"s");
				break;
				
			case 3:
				getCode.setText(R.string.get_checkCode);
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forgetpwd);
		initView();
	}
	private void initView(){
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		phoneNum=find(R.id.phoneNum);
		phoneNum.addTextChangedListener(this);
		getCode=find(R.id.getCode);
		getCode.setOnClickListener(this);
		code=find(R.id.code);
		pwd=find(R.id.pwd);
		submit=find(R.id.submit);
		submit.setOnClickListener(this);
		preference=getSharedPreferences("user", Context.MODE_PRIVATE);
		UUID=preference.getString("uuid", "");
		deviceUUID=MightypleUtil.getDeviceId(ForgetPwdActivity.this);
	}
	
	private class MyTimerTask extends TimerTask{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			timeOut--;
			if(timeOut==0){
				timeOut=60;
				handler.sendEmptyMessage(3);
				timer.cancel();
				timer=null;
			}else{
				handler.sendEmptyMessage(2);
			}
		}
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;

		case R.id.getCode:
			phoneNumber=phoneNum.getText().toString().trim();
			if(new CheckInfoUtils().isPhoneNumberValid(phoneNumber)){
				RequestMsg requestMsg=new RequestMsg("",deviceUUID, phoneNumber, fromType);
				new GetCodeThread(ForgetPwdActivity.this, handler, requestMsg).start();
			}else{
				Toast.makeText(ForgetPwdActivity.this, "手机号码格式不正确，请重新输入", Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.submit:
			phoneNumber=phoneNum.getText().toString().trim();
			password=pwd.getText().toString().trim();
			verifyCode=code.getText().toString().trim();
			if(password.equals("")){
				Toast.makeText(ForgetPwdActivity.this, "密码不能为空!", Toast.LENGTH_LONG).show();
			}else{
				dialog=ProgressDialog.show(this, null, "信息提交…", false, false);
				RequestForgetpwd requestForgetpwd=new RequestForgetpwd(UUID,deviceUUID, phoneNumber, MD5Utils.getMD5(password), deviceModel, verifyCode);
				new ForgetpwdThread(ForgetPwdActivity.this, handler, requestForgetpwd).start();
			}
			
			break;

		default:
			break;
		}
	}
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if(phoneNum.getText().toString().trim().length()>=11){
			getCode.setClickable(true);
			getCode.setTextColor(getResources().getColor(color.colorTextOrange));
		}else{
			getCode.setClickable(false);
			getCode.setTextColor(color.colorTextGrey);
		}
	}
}
